package com.youhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youhome.mapper.BClientMapper;
import com.youhome.model.BClient;
import com.youhome.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by chen365 on 2018/09/17.
 */
@Service
public class ClientServiceImpl implements ClientService {
    @Autowired
    private BClientMapper bClientMapper;

    @Override
    public int deleteByPrimaryKey(Long clientId) {
        return bClientMapper.deleteByPrimaryKey(clientId);
    }

    @Override
    public int insert(BClient record) {
        record.setStatus(1); //开启
        record.setCtime(new Date());
        return bClientMapper.insert(record);
    }

    @Override
    public int insertSelective(BClient record) {
        record.setStatus(1); //开启
        record.setCtime(new Date());
        return bClientMapper.insertSelective(record);
    }

    @Override
    public BClient selectByPrimaryKey(Long clientId) {
        return bClientMapper.selectByPrimaryKey(clientId);
    }

    @Override
    public PageInfo<BClient> selectByBean(Integer pageNumber, Integer pageSize, BClient record) {
        if (StringUtils.isEmpty(pageNumber) || pageNumber < 1) {
            pageNumber = 1;
        }
        if (StringUtils.isEmpty(pageSize) || pageSize < 10) {
            pageSize = 10;
        }
        PageHelper.startPage(pageNumber, pageSize);
        List<BClient> bClients = bClientMapper.selectByBean(record);
        return new PageInfo<>(bClients);
    }

    @Override
    public int updateByPrimaryKeySelective(BClient record) {
        record.setUtime(new Date());
        return bClientMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BClient record) {
        record.setUtime(new Date());
        return bClientMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByClientIdAndCounselorId(BClient record) {
        record.setUtime(new Date());
        return bClientMapper.updateByClientIdAndCounselorId(record);
    }

    @Override
    public int deleteByClientIdAndCounselorId(BClient bClient) {
        return bClientMapper.deleteByClientIdAndCounselorId(bClient);
    }

    @Override
    public boolean selectExistsByPrimaryKey(Long clientId) {
        if (bClientMapper.selectExistsByPrimaryKey(clientId) > 0) {
            return true;
        }
        return false;
    }
}
